//
//  ServerSettingVC.m
//  LazyHome
//
//  Created by yh on 2018/5/14.
//  Copyright © 2018年 yh. All rights reserved.
//

#import "ServerSettingVC.h"
#import "ServerModel.h"
#import "LHServerManager.h"
#import "ServerDetailVC.h"

@interface ServerSettingVC ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ServerSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    
    UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)];
    
    UIBarButtonItem *edit = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(edit)];
    self.navigationItem.rightBarButtonItems = @[add ,edit];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)add{
    ServerDetailVC *settingVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ServerDetailVC"];
    settingVC.hidesBottomBarWhenPushed = YES;
    settingVC.serverDetailMode = ServerDetailModeAdd;
    settingVC.callBack = ^(ServerModel *serverModel, ServerDetailMode serverDetailMode) {
        [[LHServerManager manager] addServer:serverModel];
        [self.tableView reloadData];
    };
    [self.navigationController pushViewController:settingVC animated:YES];
}

- (void)edit{
    [self.tableView setEditing:!self.tableView.isEditing];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [LHServerManager manager].servers.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ServerModel *serverModel = [LHServerManager manager].servers[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"名称:%@,ip:%@,host:%@",serverModel.serverName,serverModel.host,serverModel.port];
    cell.accessoryType = [serverModel.serverId isEqualToString:[LHServerManager manager].currentServerModel.serverId] ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ServerModel *serverModel = [LHServerManager manager].servers[indexPath.row];
    if (tableView.isEditing) {
        ServerDetailVC *settingVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ServerDetailVC"];
        settingVC.hidesBottomBarWhenPushed = YES;
        settingVC.serverDetailMode = ServerDetailModeEdit;
        settingVC.serverModel = serverModel;
        settingVC.callBack = ^(ServerModel *serverModel, ServerDetailMode serverDetailMode) {
            [[LHServerManager manager] modifyServer:serverModel];
            
            [tableView reloadData];
        };
        [self.navigationController pushViewController:settingVC animated:YES];
    }
    else{
        ServerDetailVC *settingVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ServerDetailVC"];
        settingVC.hidesBottomBarWhenPushed = YES;
        settingVC.serverDetailMode = ServerDetailModeNone;
        settingVC.serverModel = serverModel;
        settingVC.callBack = ^(ServerModel *serverModel, ServerDetailMode serverDetailMode) {
            [[LHServerManager manager] modifyServer:serverModel];
            
            [tableView reloadData];
        };
        [self.navigationController pushViewController:settingVC animated:YES];
    }
     [tableView reloadData];
   
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    ServerModel *serverModel = [LHServerManager manager].servers[indexPath.row];
   if(editingStyle == UITableViewCellEditingStyleDelete) {
        [[LHServerManager manager] removeServer:serverModel];
        [tableView reloadData];
    }else{
        
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
